#include "types.h"
#include "global.h"
#include "board.h"
#include "ms_reg.h"
#include "drvOSD.h"
#include "ms_rwreg.h"
//#include "MenuStr.h"
#include "ColorPalette.h"
#include "msosd.h"
#include "menudef.h"

#if CHIP_ID==CHIP_TSUMV
extern BYTE xdata g_u8OsdWndNo;
#endif
extern BYTE xdata OsdFontColor;

extern xdata BYTE MenuPageIndex;
extern xdata BYTE MenuItemIndex;

BYTE code ASCII2FONT[256] =  /* OSD character table */
{
    /*           0        1        2          3   4        5        6        7   8        9       A         B          C       D        E        F   */
    /*00H*/0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //                     " "
    /*10H*/0x19, 0x02, 0x27, 0x1c, 0x27, 0x28, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //                     "-"     " . "
    /*20H*/0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x00,
    //
    /*30H*/0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //             0        1        2       3        4        5        6        7        8       9
    /*40H*/0x00, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
    //                      A        B        C       D        E        F        G       H        I        J        K        L        M       N        O
    /*50H*/0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00,
    //             P        Q       R        S       T        U        V       W       X        Y       Z
    /*60H*/0x00, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
    //                      a        b        c        d        e       f         g        h        i         j        k        l         m       n        o
    /*70H*/0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00,
    //            p       q         r        s        t         u       v        w       x        y        z
    /*80H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*90H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*A0H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*B0H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*C0H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*D0H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*E0H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
    /*F0H*/0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //
};

BYTE code tSpace7[] =
{
    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, '\0'
};


void Osd_DrawStr( BYTE xPos, BYTE yPos, BYTE *str )
{
#if CHIP_ID>=CHIP_TSUMV
    WORD u16TempValue;
    BYTE u8DrawCode = (!(yPos & BIT7));

    yPos &= 0x7F;
    u16TempValue=GET_TXT_CASTART(g_u8OsdWndNo,xPos,yPos); // get real address
    
#define dispChar    xPos
#define dispPtr     yPos
    dispPtr = 0;
    WRITE_CAFSRAM_ADDR();
    msWrite2Byte(PORT_ATTR_ADDR, u16TempValue); // display font attribute
    WRITE_ATTRIBUTE();
    while( *( str + dispPtr ) )        // display string font index
    {
        msWriteByte(PORT_ATTR_DATA, OsdFontColor);
        dispPtr++;
    }
    if( u8DrawCode )
    {
        dispPtr = 0;
	 WRITE_CAFSRAM_ADDR();
        msWrite2Byte(PORT_CODE_ADDR, u16TempValue); // dispaly font code
        WRITE_CODE();
        while( dispChar = *( str + dispPtr ) )            // display string font index
        {
            msWriteByte(PORT_CODE_DATA, ASCII2FONT[dispChar]);
            dispPtr++;
        }
    }
#undef dispChar
#undef dispPtr
#else
    
    WORD tempValue;
    BYTE drawCode = (!(yPos & BIT7));   
	
    yPos &= 0x7F; //~(BIT7|BIT6);
#define dispChar    xPos
#define dispPtr     yPos
    tempValue = ( WORD )yPos * OsdWindowWidth + xPos; // get real address
    dispPtr = 0;
    msWrite2Byte( OSD2_A8, tempValue );
    while( dispChar = *( str + dispPtr ) )
        // display string font index
    {
        msWriteByte( OSD2_AA, OsdFontColor );
        dispPtr++;
    }
    if( drawCode )
    {
        dispPtr = 0;
        msWrite2Byte( OSD2_A5, tempValue );
        while( dispChar = *( str + dispPtr ) )            // display string font index
        {
            msWriteByte( OSD2_A7, ASCII2FONT[dispChar] );
            dispPtr++;
        }
    }
#undef dispChar
#undef dispPtr
   
#endif

}



